/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import javax.swing.event.MouseInputListener;

public class Input implements KeyListener, MouseInputListener {
    
    private static HashMap<Integer, KeyInfo> keys = new HashMap<Integer, KeyInfo>();
    private static int mouseX, mouseY;
    private static boolean mouseDown, mouseWindow;

    public void keyTyped(KeyEvent e) {}

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        KeyInfo tmp = keys.containsKey(key) ? keys.get(key) : new KeyInfo();
        tmp.isPressed = true;
        keys.put(e.getKeyCode(), tmp);
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        KeyInfo tmp = keys.containsKey(key) ? keys.get(key) : new KeyInfo();
        tmp.isPressed = false;
        keys.put(e.getKeyCode(), tmp);
    }

    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
        if (e.getButton() != MouseEvent.BUTTON1)
            return;
        mouseDown = true;
    }

    public void mouseReleased(MouseEvent e) {
        if (e.getButton() != MouseEvent.BUTTON1)
            return;
        mouseDown = false;
    }

    public void mouseEntered(MouseEvent e) { mouseWindow = true; }

    public void mouseExited(MouseEvent e) { mouseWindow = false; }

    public void mouseDragged(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }

    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }

    public static boolean isPressed(int key) {
        return isPressed(key, 5);
    }

    public static boolean isPressed(int key, int timeout) {
        if (!keys.containsKey(key)) return false;
        KeyInfo info = keys.get(key);
        if (info.timeout > 0) info.timeout--;
        if (info.timeout > 0 && timeout > -1) return false;
        if (info.isPressed && timeout > -1) info.timeout = timeout;
        return info.isPressed;
    }

    public static MouseInfo getMouseInfo() {
        MouseInfo i = new MouseInfo();
        i.x = mouseX; i.y = mouseY;
        i.buttonDown = mouseDown;
        i.mouseInWindow = mouseWindow;
        return i;
    }

    public static class MouseInfo {
        public int x, y;
        public boolean buttonDown, mouseInWindow;
    }

    private static class KeyInfo {
        boolean isPressed;
        int timeout;
    }
}